package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.domain.entity.DetailTabConfigEntity;
import com.xbongbong.saas.domain.dao.DetailTabConfigDao;
import com.xbongbong.saas.model.DetailTabConfigModel;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author feng.zheng
 * @version v1.0
 * @date 2019/6/22 15:11
 * @since v1.0
 */
@Service("detailTabConfigModel")
public class DetailTabConfigModelImpl implements DetailTabConfigModel {
    @Resource
    private DetailTabConfigDao detailTabConfigDao;

    @Override
    public Integer insert(DetailTabConfigEntity entity) throws XbbException {
        entity.setAddTime(DateTimeUtil.getInt());
        entity.setUpdateTime(DateTimeUtil.getInt());
        return detailTabConfigDao.insert(entity);
    }

    @Override
    public Integer update(DetailTabConfigEntity entity) throws XbbException {
        entity.setUpdateTime(DateTimeUtil.getInt());
        return detailTabConfigDao.update(entity);
    }

    @Override
    public List<DetailTabConfigEntity> findEntitys(Map<String, Object> param) {
        return detailTabConfigDao.findEntitys(param);
    }

    @Override
    public DetailTabConfigEntity getEntityByBusinessTypeAndFormId(String corpid, String userId, Integer businessType, Long formId) {
        return detailTabConfigDao.getEntityByBusinessTypeAndFormId(corpid, userId, businessType, formId);
    }

    @Override
    public Integer save(DetailTabConfigEntity entity) throws XbbException {
        if (entity.getId() == null || Objects.equals(entity.getId(), 0L)) {
            entity.setId(null);
            return insert(entity);
        }
        return update(entity);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return detailTabConfigDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public DetailTabConfigEntity getByKey(Long key, String corpid) {
        return detailTabConfigDao.getByKey(key, corpid);
    }

    @Override
    public Integer deleteByKey(Long key, String corpid) throws XbbException {
        return detailTabConfigDao.deleteByKey(key, corpid);
    }
    
    /**
     * 处理批量更新(合同、客户的tab)
     *
     * @param list
     * @throws XbbException
     */
    @Override
    public Integer updateBatch(List<DetailTabConfigEntity> list) throws XbbException {
        return detailTabConfigDao.updateBatch(list);
    }

    @Override
    public Integer insertBatch(List<DetailTabConfigEntity> insertList) {
        if (CollectionUtils.isEmpty(insertList)) {
            return 0;
        }
        return detailTabConfigDao.insertBatch(insertList);
    }
}
